
************************************************************************************************
* Constructing data set to "Public Health Policy At Scale.."
************************************************************************************************

************************************************************************************************
* Full population born 1970-2013
* Including maternal socioeconomic variables 
*************************************************************************************************

global rawStata2 "X:\Data\Workdata\707116\Stata2"
global rawStata "X:\Data\Workdata\707116\Stata"
global data "X:\Data\Workdata\707116\ZJI\data"
global temp "X:\Data\Workdata\707116\ZJI\temp"
global figures "X:\Data\Workdata\707116\ZJI\figures"
global formater "X:\Data\Workdata\707116\ZJI\formater"
global temp2 "X:\Data\Workdata\707116\ChildVax\02_temp\Nov2019" 

set mem 2g
set more off
set linesize 255


clear all


*******************************************************************************************
* For cohorts 1973-1981 parental characteristics are from 1980                            *
* because registry information on parental SES does not exist before                      *
* Parental information: we include immigrant status, marital status, and education        *
* Maternal age can be be included (foed_dag) but is also included in the birth registries *
*******************************************************************************************


use $temp2\lpr1996.dta
keep if (byear>=1973 & byear<=1981) 

tempfile t1 
save `t1' 

use `t1'
bysort mpnr_mfr: g i=_n
keep if i==1 //keep only unique id of moms 
keep mpnr_mfr
rename mpnr_mfr mor_id 
tempfile t2
save `t2' 


******************************************************
* population - mothers in grundvive 1980             * 
* characteristics in 1980                            *
******************************************************

use pnr koen IE_TYPE C_STATUS hfaud using $rawStata2\grundvive1980.dta

keep if koen==2
rename pnr mor_id

sort mor_id

merge  1:1 mor_id using `t2' 

keep if _merge==3 // keep matched mothers 
drop if mor_id==""

drop _merge

gen grundviveyear = 1980


/** married, registred partner or living together (with or without children) ******/

gen m_couple=0
replace m_couple=1 if (C_STATUS==2)
replace m_couple=1 if (C_STATUS>=4 & C_STATUS<=7)

/* immigrants: Western and non-Western (1. and 2. generation) immigrants  */

gen m_immigrant=0
replace m_immigrant=1 if (IE_TYPE==2|IE_TYPE==3)


*** formatting education categories ***

gen start = hfaud
replace start=20 if hfaud==.

sort start

merge m:1 start using $formater\audd2008.dta 
drop _merge			
drop start

*basic is less than 9 years
gen m_basic_edu = 0
replace m_basic_edu=1 if (AUDD2008_L1L5_KT < "20" | AUDD2008_L1L5_KT >="90")
* vocational and highschool
gen m_voc_high_edu= (AUDD2008_L1L5_KT >="20" & AUDD2008_L1L5_KT <"40")

gen m_short_furt_edu= (AUDD2008_L1L5_KT >="40" & AUDD2008_L1L5_KT <"50")
gen m_mid_furt_edu= (AUDD2008_L1L5_KT >="50" & AUDD2008_L1L5_KT <"60")
gen m_long_furt_edu= (AUDD2008_L1L5_KT >="60" & AUDD2008_L1L5_KT <"90")

rename mor_id mpnr_mfr

drop if mpnr_mfr==""

keep mpnr_mfr grundviveyear m_*

sort mpnr_mfr


save $temp2\mother_1980.dta, replace 


*******************************************************************************************
* For cohorts 1980-2006 parental characteristics are from grundvive(t-1)                  *
* Parental information: we include age, immigrant status, marital status, and education   *
*******************************************************************************************

clear 

forval t= 1982/2006 {

local t_1 = `t' - 1

use $temp2\lpr1996.dta
append using $temp2\lpr2014.dta
bysort pnr: g j=_N 
drop if j>1
drop j
keep if byear==`t'
keep pnr mpnr_mfr  
tempfile t3 
save `t3' 

use `t3'
bysort mpnr_mfr: g i=_n
keep if i==1 //keep only unique id of moms 
keep mpnr_mfr
rename mpnr_mfr mor_id 
tempfile t4
save `t4' 


******************************************************
* population - mothers in grundvive                  * 
* characteristics in the year before birth year      *
******************************************************

use pnr koen IE_TYPE C_STATUS hfaud using $rawStata2\grundvive`t_1'.dta

keep if koen==2
rename pnr mor_id

sort mor_id

merge  1:1 mor_id using `t4' 

tab _merge 

keep if _merge==3 // keep matched mothers 
drop if mor_id==""

gen grundviveyear = `t_1'


/** married, registred partner or living together (with or without children) ******/

gen m_couple=0
replace m_couple=1 if (C_STATUS==2)
replace m_couple=1 if (C_STATUS>=4 & C_STATUS<=7)

/* immigrants: Western and non-Western (1. and 2. generation) immigrants  */

gen m_immigrant=0
replace m_immigrant=1 if (IE_TYPE==2|IE_TYPE==3)


*** formatting education categories ***

gen start = hfaud
replace start=20 if hfaud==.
drop _merge
sort start

merge m:1 start using $formater\audd2008.dta 
drop _merge			
drop start

*basic is less than 9 years
gen m_basic_edu = 0
replace m_basic_edu=1 if (AUDD2008_L1L5_KT < "20" | AUDD2008_L1L5_KT >="90")
* vocational and highschool
gen m_voc_high_edu= (AUDD2008_L1L5_KT >="20" & AUDD2008_L1L5_KT <"40")

gen m_short_furt_edu= (AUDD2008_L1L5_KT >="40" & AUDD2008_L1L5_KT <"50")
gen m_mid_furt_edu= (AUDD2008_L1L5_KT >="50" & AUDD2008_L1L5_KT <"60")
gen m_long_furt_edu= (AUDD2008_L1L5_KT >="60" & AUDD2008_L1L5_KT <"90")

rename mor_id mpnr_mfr
drop if mpnr_mfr==""
keep mpnr_mfr grundviveyear m_*

sort mpnr_mfr

save $temp2\mother_`t_1'.dta, replace 
}

use $temp2\mother_1980,clear 
forval t= 1981/2005 {
append using $temp2\mother_`t'
}

save $temp2\maternal_ses.dta, replace 



